﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using FSS;
using System;
using System.IO;

public delegate void F_OnReloadSimpleBattleConfigFileDone();
namespace FSS
{
    public class ConfigManager : UnitySingleton<ConfigManager>
    {
        public bool Ready { get { return ConfigIsReady; } }
        //public GlobalConfig GlobalCfg { get { return mGlobalConfig; } }
        const string mConfigRootPath = "/config/";
        const string mJsonDir = "json/";
        const string mCSVDir = "csv/";
        const string mXMLDir = "xml/";
        string mConfigFiles = "all_config.json";
        bool ConfigIsReady = false;
        bool Loading = false;

        public string JsonConfigPath
        {
            get
            {
                string strPath = mConfigRootPath + mJsonDir;
                return strPath;
            }
        }

        public string WritableJsonConfigPath
        {
            get
            {
                //Debug.Log("datapath:" + Application.persistentDataPath);
                string strDir = Application.persistentDataPath + mConfigRootPath + mJsonDir;
                if (!Directory.Exists(strDir))
                    Directory.CreateDirectory(strDir);
                strDir += Path.DirectorySeparatorChar;
                return strDir;
            }
        }

        public string CSVConfigPath
        {
            get
            {
                string strPath = mConfigRootPath + mCSVDir;
                return strPath;
            }
        }

        public string WritableCSVConfigPath
        {
            get
            {
                //Debug.Log("datapath:" + Application.persistentDataPath);
                string strDir = Application.persistentDataPath + mConfigRootPath + mCSVDir;
                if (!Directory.Exists(strDir))
                    Directory.CreateDirectory(strDir);
                strDir += Path.DirectorySeparatorChar;
                return strDir;
            }
        }

        public string WritableXMLConfigPath
        {
            get
            {
                //Debug.Log("datapath:" + Application.persistentDataPath);
                string strDir = Application.persistentDataPath + mConfigRootPath + mXMLDir;
                if (!Directory.Exists(strDir))
                    Directory.CreateDirectory(strDir);
                strDir += Path.DirectorySeparatorChar;
                return strDir;
            }
        }

        public bool LoadConfig()
        {
            if (!ConfigIsReady && !Loading)
            {
                StartCoroutine(LoadFirstConfigFile());
                Loading = true;
            }
            return ConfigIsReady;
        }


        public IEnumerator LoadFirstConfigFile()
        {
            string strFileName = GetWWWPath(mConfigRootPath) + mConfigFiles;
            WWW www;
            System.IO.StringReader stringReader;
            string strContent;
            www = new WWW(strFileName);
            yield return www;
            stringReader = new System.IO.StringReader(www.text);
            strContent = stringReader.ReadToEnd();
            ConfigFilesConfigMgr.Instance.mConfig = JsonUtility.FromJson<ConfigFilesConfig>(strContent);

            LoadConfigFiles();

            ConfigIsReady = true;
        }

        public void LoadConfigFiles()
        {
            ConfigFilenames currentConfigFiles = ConfigFilesConfigMgr.Instance.mConfig.SchemaFiles[ConfigFilesConfigMgr.Instance.mConfig.SchemeSelected];
            StartCoroutine(GlobalConfigMgr.Instance.LoadFromFile(GetWWWPath(JsonConfigPath + currentConfigFiles.GlobalConfigFile)));

            BuidingTypeConfigMgr.Instance.LoadFromFile(CSVConfigPath + currentConfigFiles.BuildingTypesConfigFile);

            //         strFileName = GetWWWPath(JsonConfigPath + mGlobalConfig.CfgSet.SchemaFiles[mGlobalConfig.CfgSet.Schema].CameraPosPresetFile);
            //         www = new WWW(strFileName);
            //         yield return www;
            //         stringReader = new System.IO.StringReader(www.text);
            //         strContent = stringReader.ReadToEnd();
            //         CameraPosPreset cpp1 = JsonUtility.FromJson<CameraPosPreset>(strContent);
            //         CameraPosPresetMgr.Instance.mCamerPosPrest1 = cpp1;
            // 
            //         strFileName = GetWWWPath(JsonConfigPath + mGlobalConfig.CfgSet.SchemaFiles[mGlobalConfig.CfgSet.Schema].CameraPosPresetFile2);
            //         www = new WWW(strFileName);
            //         yield return www;
            //         stringReader = new System.IO.StringReader(www.text);
            //         strContent = stringReader.ReadToEnd();
            //         CameraPosPreset cpp2 = JsonUtility.FromJson<CameraPosPreset>(strContent);
            //         CameraPosPresetMgr.Instance.mCamerPosPrest2 = cpp2;
            // 
            //         strFileName = GetWWWPath(JsonConfigPath + mGlobalConfig.CfgSet.SchemaFiles[mGlobalConfig.CfgSet.Schema].WarehouseConfigFile);
            //         www = new WWW(strFileName);
            //         yield return www;
            //         stringReader = new System.IO.StringReader(www.text);
            //         strContent = stringReader.ReadToEnd();
            //         WarehouseConfig wc = JsonUtility.FromJson<WarehouseConfig>(strContent);
            //         WarehouseConfigMgr.Instance.mConfig = wc;
            // 
            //         strFileName = GetWWWPath(JsonConfigPath + mGlobalConfig.CfgSet.SchemaFiles[mGlobalConfig.CfgSet.Schema].SimpleBattleConfigFile);
            //         www = new WWW(strFileName);
            //         yield return www;
            //         stringReader = new System.IO.StringReader(www.text);
            //         strContent = stringReader.ReadToEnd();
            //         SimpleBattleConfig sbc = JsonUtility.FromJson<SimpleBattleConfig>(strContent);
            //         SimpleBattleConfigMgr.Instance.mConfig = sbc;
            // 
            //         strFileName = GetWWWPath(JsonConfigPath + mGlobalConfig.CfgSet.SchemaFiles[mGlobalConfig.CfgSet.Schema].SimpleBattleTimelineConfigFile);
            //         www = new WWW(strFileName);
            //         yield return www;
            //         stringReader = new System.IO.StringReader(www.text);
            //         strContent = stringReader.ReadToEnd();
            //         SimpleBattleTimelineConfig sbtc = JsonUtility.FromJson<SimpleBattleTimelineConfig>(strContent);
            //         SimpleBattleTimelineConfigMgr.Instance.mTimelineConfig = sbtc;
            // 
            //         strFileName = GetWWWPath(JsonConfigPath + mGlobalConfig.CfgSet.SchemaFiles[mGlobalConfig.CfgSet.Schema].CameraTrembleConfigFile);
            //         www = new WWW(strFileName);
            //         yield return www;
            //         stringReader = new System.IO.StringReader(www.text);
            //         strContent = stringReader.ReadToEnd();
            //         CameraTrembleConfig ctc = JsonUtility.FromJson<CameraTrembleConfig>(strContent);
            //         SimpleBattleTimelineConfigMgr.Instance.mTrembleConfig = ctc;
            // 
            //         strFileName = GetWWWPath(JsonConfigPath + mGlobalConfig.CfgSet.SchemaFiles[mGlobalConfig.CfgSet.Schema].BattleSpeedSchemeConfigFile);
            //         www = new WWW(strFileName);
            //         yield return www;
            //         stringReader = new System.IO.StringReader(www.text);
            //         strContent = stringReader.ReadToEnd();
            //         BattleSpeedSchemeConfig bssc = JsonUtility.FromJson<BattleSpeedSchemeConfig>(strContent);
            //         SimpleBattleTimelineConfigMgr.Instance.mSpeedConfig = bssc;
            // 
            //         strFileName = GetWWWPath(JsonConfigPath + mGlobalConfig.CfgSet.SchemaFiles[mGlobalConfig.CfgSet.Schema].BattleTriggerConfigFile);
            //         www = new WWW(strFileName);
            //         yield return www;
            //         stringReader = new System.IO.StringReader(www.text);
            //         strContent = stringReader.ReadToEnd();
            //         BattleTriggerConfig btc = JsonUtility.FromJson<BattleTriggerConfig>(strContent);
            //         SimpleBattleTimelineConfigMgr.Instance.mTriggerConfig = btc;

            //         if (doneFunc!=null)
            //         {
            //             doneFunc();
            //         }
        }

        public void SaveGlobalConfigJson(string fileName)
        {

        }

        private string GetWWWPath(string strPath="/")
        {
            if (Application.platform == RuntimePlatform.Android)
            {
                return Application.streamingAssetsPath + strPath;
            }
            else
            {
                return "file://" + Application.streamingAssetsPath + strPath;
            }
        }

        void LoadCsvFiles()
        {
            //         if(idx>=mGlobalConfig.CfgSet.SchemaFiles.Length)
            //         {
            //             idx = 0;
            //         }
            //         if(idx<mGlobalConfig.CfgSet.SchemaFiles.Length)
            //         {
            //             CSVHelper.Instance().ReadCSVFile(mGlobalConfig.CfgSet.SchemaFiles[idx].BuildingType, (table) => {
            //                 foreach (CSVLine line in table)// 遍历数据
            //                 {
            //                     foreach (KeyValuePair<string, string> item in line)
            //                     {
            //                         //Debug.Log(string.Format("item key = {0} item value = {1}", item.Key, item.Value));
            //                     }
            //                 }
            //                 //可以拿到表中任意一项数据
            //                 //Debug.Log(table["10011"]["id"]);
            //             });
            // 
            //             CSVHelper.Instance().ReadCSVFile(mGlobalConfig.CfgSet.SchemaFiles[idx].EnemyTypes, (table) => {
            //                 foreach (CSVLine line in table)// 遍历数据
            //                 {
            //                     foreach (KeyValuePair<string, string> item in line)
            //                     {
            //                         //Debug.Log(string.Format("item key = {0} item value = {1}", item.Key, item.Value));
            //                     }
            //                 }
            //                 //可以拿到表中任意一项数据
            //                 //Debug.Log(table["10011"]["id"]);
            //             });
            // 
            //             CSVHelper.Instance().ReadCSVFile(mGlobalConfig.CfgSet.SchemaFiles[idx].CameraSpotPos, (table) =>
            //             {
            //                 foreach (CSVLine line in table)// 遍历数据
            //                 {
            //                     CameraSpotInfo csi = new CameraSpotInfo();
            //                     CameraSpotInfo.ParseLine(line, csi);
            //                     CameraSpotInfoMgr.Instance.Add(csi);
            //                     //LstCameraSpotInfo.Add(csi);
            // //                     foreach (KeyValuePair<string, string> item in line)
            // //                     {
            // //                         // Debug.Log(string.Format("CameraSpotPos item key = {0} item value = {1}", item.Key, item.Value));
            // //                     }
            //                 }
            //                 //可以拿到表中任意一项数据
            //                 //Debug.Log(table["10011"]["id"]);
            //             });
            // 
            //             CSVHelper.Instance().ReadCSVFile(mGlobalConfig.CfgSet.SchemaFiles[idx].CameraSpotInspire, (table) =>
            //             {
            //                 foreach (CSVLine line in table)// 遍历数据
            //                 {
            //                     CameraSpotInfo csi = new CameraSpotInfo();
            //                     CameraSpotInfo.ParseLine(line, csi);
            //                     CameraSpotInfoMgr.Instance.AddInspire(csi);
            //                 }
            //             });
            // 
            //             CSVHelper.Instance().ReadCSVFile(mGlobalConfig.CfgSet.SchemaFiles[idx].GundamPart, (table) =>
            //             {
            //                 foreach (CSVLine line in table)
            //                 {
            //                     //GundamPartInfo gpi = new GundamPartInfo();
            //                     CustomCharacterMgr.Instance.ParseLine(line);
            //                 }
            //             });
            //         }
        }

    }
}